package com.video.portlet.action;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletConfig;
import javax.portlet.PortletPreferences;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import com.liferay.portal.kernel.portlet.BaseConfigurationAction;
import com.liferay.portal.kernel.servlet.SessionMessages;
import com.liferay.portal.kernel.util.Constants;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portlet.PortletPreferencesFactoryUtil;

public class ConfigurationActionImpl extends BaseConfigurationAction {
	public void processAction(
			PortletConfig portletConfig, ActionRequest actionRequest,
			ActionResponse actionResponse)
		throws Exception {

		String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
		String videoFrom = ParamUtil.getString(actionRequest, "videoFrom");	
		
		if(com.video.portlet.Constants.YOUTUBE_UPDATE.equalsIgnoreCase(cmd)){
			manage(portletConfig, actionRequest, actionResponse, com.video.portlet.Constants.YOUTUBE);
			videoFrom = com.video.portlet.Constants.YOUTUBE;
		} else if(com.video.portlet.Constants.VIMEO_UPDATE.equalsIgnoreCase(cmd)){
			manage(portletConfig, actionRequest, actionResponse, com.video.portlet.Constants.VIMEO);
			videoFrom = com.video.portlet.Constants.VIMEO;
		} else if(com.video.portlet.Constants.DAILYMOTION_UPDATE.equalsIgnoreCase(cmd)){
			manage(portletConfig, actionRequest, actionResponse, com.video.portlet.Constants.DAILYMOTION);
			videoFrom = com.video.portlet.Constants.DAILYMOTION;
		} else if(com.video.portlet.Constants.SEVENLOAD_UPDATE.equalsIgnoreCase(cmd)){
			manage(portletConfig, actionRequest, actionResponse, com.video.portlet.Constants.SEVENLOAD);
			videoFrom = com.video.portlet.Constants.SEVENLOAD;
		} else if(com.video.portlet.Constants.EDUTOPIA_UPDATE.equalsIgnoreCase(cmd)){
			manage(portletConfig, actionRequest, actionResponse, com.video.portlet.Constants.EDUTOPIA);
			videoFrom = com.video.portlet.Constants.EDUTOPIA;
		} else if(com.video.portlet.Constants.REVVER_UPDATE.equalsIgnoreCase(cmd)){
			manage(portletConfig, actionRequest, actionResponse, com.video.portlet.Constants.REVVER);
			videoFrom = com.video.portlet.Constants.REVVER;
		} else if(com.video.portlet.Constants.FUNNYORDIE_UPDATE.equalsIgnoreCase(cmd)){
			manage(portletConfig, actionRequest, actionResponse, com.video.portlet.Constants.FUNNYORDIE);
			videoFrom = com.video.portlet.Constants.FUNNYORDIE;
		} else if("change_settings".equalsIgnoreCase(cmd)){
			String portletResource = ParamUtil.getString(
					actionRequest, "portletResource");

			PortletPreferences preferences =
				PortletPreferencesFactoryUtil.getPortletSetup(
					actionRequest, portletResource);

			preferences.setValue("url", StringPool.BLANK);
			preferences.setValue("videoFrom", StringPool.BLANK);
			preferences.setValue("autoplay", StringPool.BLANK);
			preferences.setValue("enableFullscreen", StringPool.BLANK);
			preferences.setValue("height", "360");
			preferences.setValue("width", "480");
			preferences.setValue("annotations", StringPool.BLANK);
			preferences.setValue("borderColor", StringPool.BLANK);
			preferences.setValue("enableEnhancedGenieMenu", StringPool.BLANK);
			preferences.setValue("enableKeyboardControls", StringPool.BLANK);
			preferences.setValue("enableRelatedVideos", StringPool.BLANK);
			preferences.setValue("enableSearch", StringPool.BLANK);
			preferences.setValue("hd", StringPool.BLANK);
			preferences.setValue("loop", StringPool.BLANK);
			preferences.setValue("showInfo", StringPool.BLANK);
			preferences.setValue("showThickerBorder", StringPool.BLANK);
			preferences.setValue("showTitle", StringPool.BLANK);
			preferences.store();
		}	
		
		actionRequest.setAttribute("videoFrom", videoFrom);
	}
	
	public String render(
			PortletConfig portletConfig, RenderRequest renderRequest,
			RenderResponse renderResponse)
		throws Exception {
		
		String videoFrom = (String) renderRequest.getAttribute("videoFrom");
		
		if (com.video.portlet.Constants.YOUTUBE.equalsIgnoreCase(videoFrom)) {
			return "/html/youtube/configuration.jsp";
		} else if (com.video.portlet.Constants.VIMEO
				.equalsIgnoreCase(videoFrom)) {
			return "/html/vimeo/configuration.jsp";
		} else if (com.video.portlet.Constants.DAILYMOTION
				.equalsIgnoreCase(videoFrom)) {
			return "/html/dailymotion/configuration.jsp";
		} else if (com.video.portlet.Constants.SEVENLOAD
				.equalsIgnoreCase(videoFrom)) {
			return "/html/sevenload/configuration.jsp";
		} else if (com.video.portlet.Constants.EDUTOPIA
				.equalsIgnoreCase(videoFrom)) {
			return "/html/edutopia/configuration.jsp";
		} else if (com.video.portlet.Constants.REVVER
				.equalsIgnoreCase(videoFrom)) {
			return "/html/revver/configuration.jsp";
		} else if (com.video.portlet.Constants.FUNNYORDIE
				.equalsIgnoreCase(videoFrom)) {
			return "/html/funnyordie/configuration.jsp";
		}
		
		return "/html/common/configuration.jsp";
	}
	
	private void manage(
			PortletConfig portletConfig, ActionRequest actionRequest,
			ActionResponse actionResponse, String videoFrom)
		throws Exception {				
		
		boolean autoplay = ParamUtil.getBoolean(actionRequest, "autoplay");
		boolean enableFullscreen = ParamUtil.getBoolean(
			actionRequest, "enableFullscreen");
		String height = ParamUtil.getString(actionRequest, "height");
		String playerColor = ParamUtil.getString(actionRequest, "playerColor");		
		String url = ParamUtil.getString(actionRequest, "url");
		String width = ParamUtil.getString(actionRequest, "width");
		
		String portletResource = ParamUtil.getString(
				actionRequest, "portletResource");

		PortletPreferences preferences =
			PortletPreferencesFactoryUtil.getPortletSetup(
				actionRequest, portletResource);
		
		if(com.video.portlet.Constants.YOUTUBE.equalsIgnoreCase(videoFrom)){
			
			String annotations = ParamUtil.getString(actionRequest, "annotations"); 
			String borderColor = ParamUtil.getString(actionRequest, "borderColor");
			boolean enableEnhancedGenieMenu = ParamUtil.getBoolean(
				actionRequest, "enableEnhancedGenieMenu");
			boolean enableKeyboardControls = ParamUtil.getBoolean(
				actionRequest, "enableKeyboardControls"); 
			boolean enableRelatedVideos = ParamUtil.getBoolean(
				actionRequest, "enableRelatedVideos");
			boolean enableSearch = ParamUtil.getBoolean(
				actionRequest, "enableSearch");	
			boolean hd = ParamUtil.getBoolean(actionRequest, "hd");
			boolean loop = ParamUtil.getBoolean(actionRequest, "loop");
			boolean showInfo = ParamUtil.getBoolean(actionRequest, "showInfo");
			boolean showThickerBorder = ParamUtil.getBoolean(
				actionRequest, "showThickerBorder");
			String startTime = ParamUtil.getString(actionRequest, "startTime");
			
			preferences.setValue("annotations", annotations); 
			
			preferences.setValue("borderColor", borderColor);
			preferences.setValue(
				"enableEnhancedGenieMenu", String.valueOf(enableEnhancedGenieMenu));

			preferences.setValue(
				"enableKeyboardControls", String.valueOf(enableKeyboardControls));
			preferences.setValue(
				"enableRelatedVideos", String.valueOf(enableRelatedVideos));
			preferences.setValue("enableSearch", String.valueOf(enableSearch));
			preferences.setValue("loop", String.valueOf(loop));
			preferences.setValue("playerColor", playerColor);
			preferences.setValue("showInfo", String.valueOf(showInfo));
			preferences.setValue(
				"showThickerBorder", String.valueOf(showThickerBorder));
			preferences.setValue("startTime", startTime);
			preferences.setValue("hd", String.valueOf(hd));
			
		} else {
			boolean showTitle = ParamUtil.getBoolean(actionRequest, "showTitle");
			preferences.setValue("showTitle", String.valueOf(showTitle));
		}
		preferences.setValue(
				"enableFullscreen", String.valueOf(enableFullscreen));
		preferences.setValue("autoplay", String.valueOf(autoplay));
		// video from
		preferences.setValue("videoFrom", videoFrom);			
		preferences.setValue("height", height);
		preferences.setValue("url", url);
		preferences.setValue("width", width);

		preferences.store();

		SessionMessages.add(
			actionRequest, portletConfig.getPortletName() + ".doConfigure");		
	}
}

